package com.steakliu.springasync.configuration;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 功能说明：Async线程池配置
 *
 * <p>
 * Original @Author: steakliu-刘牌, 2022-06-26  11:37
 * <p>
 * Copyright (C)2020-2022  小四的技术之旅 All rights reserved.
 */
@Configuration
public class ThreadPoolConfiguration {

    @Bean
    public TaskExecutor taskExecutor(){
        /**
         * CPU核数
         */
        int processors = Runtime.getRuntime().availableProcessors();
        ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
        threadPoolTaskExecutor.setCorePoolSize(processors * 2);
        threadPoolTaskExecutor.setMaxPoolSize(processors * 4);
        threadPoolTaskExecutor.setThreadNamePrefix("async-thread-");
        threadPoolTaskExecutor.setKeepAliveSeconds(60);
        return threadPoolTaskExecutor;
    }
}
